package com.rcd.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.springframework.transaction.annotation.Transactional;

import com.rcd.dao.IProductDao;
import com.rcd.exception.MyException;
import com.rcd.util.Page;
import com.rcd.vo.Product;

@Transactional
public class IProductDaoimpl implements IProductDao {

	@Resource private SessionFactory sessionFactory;
	private Criteria cri;
	private int count;
	private List<Product> list = null;
	private Product product = null;
	public void addProduct(Product product) throws MyException {
		try {
			System.err.println(sessionFactory);
			sessionFactory.getCurrentSession().persist(product);
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error   in   addProduct",
					"IProductimpl-----------" });
		}
	}

	public void delProduct(int pid) throws MyException {
		try {
			sessionFactory.getCurrentSession().delete(
					sessionFactory.getCurrentSession().get(Product.class, pid));
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error   in   delProduct",
					"IProductimpl-----------" });
		}
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	public List<Product> findAll(Page page) throws MyException {
		try {
			cri = sessionFactory.getCurrentSession().createCriteria(Product.class);
			cri.add(Expression.like("pname","%" + page.getKeyword() + "%"));
			cri.addOrder(Order.desc(page.getCode()));
			cri.setFirstResult((page.getPagecode()*page.getPagesize())-page.getPagesize());
			cri.setMaxResults(page.getPagesize());
			list = cri.list();
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error   in   findAll",
					"IProductimpl-----------" });
		}
		return list;
	}

	@SuppressWarnings("deprecation")
	public Product findByPid(int pid) throws MyException {
		try {
			cri = sessionFactory.getCurrentSession().createCriteria(Product.class);
			cri.add(Expression.eq("pid", pid));
			product = (Product)cri.uniqueResult();
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error   in   findByPid",
					"IProductimpl-----------" });
		}
		return product;
	}

	@SuppressWarnings("deprecation")
	public int getAllSize(Page page) throws MyException {
		try {
			cri = sessionFactory.getCurrentSession().createCriteria(Product.class);
			cri.add(Expression.like("pname","%" + page.getKeyword() + "%"));
			count = cri.list().size();
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error   in   getAllSize",
					"IProductimpl-----------" });
		}
		return count;
	}

	public void updateProduct(Product product) throws MyException {
		try {
			sessionFactory.getCurrentSession().update(product);
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error   in   updateProduct",
					"IProductimpl-----------" });
		}

	}

	public void delProducts(Object[] pids) throws MyException {
		// TODO Auto-generated method stub
		
	}

}
